﻿using System;
using System.IO;
using BackupConsoleClient.BackupServiceReference;
using BackupConsoleClient.Parser;

namespace BackupConsoleClient
{
    /// <summary>
    /// Commands line arguments.
    /// </summary>
    class CommandLineArguments
    {
        /// <summary>
        /// The entity id.
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// The id of the parent tag.
        /// </summary>
        public string ParentTagId { get; private set; }

        /// <summary>
        /// The command name.
        /// </summary>
        public CommandType Command { get; private set; }

        /// <summary>
        /// The name of the user.
        /// </summary>
        public string User { get; private set; }

        /// <summary>
        /// The password for the given user.
        /// </summary>
        public string Password { get; private set; }

        /// <summary>
        /// The location of the file.
        /// </summary>
        public string File { get; private set; }

        /// <summary>
        /// The name of the file from backup system.
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// A description for the file.
        /// </summary>
        public string Description { get; private set; }

        /// <summary>
        /// The vizibility of the file that is uploaded.
        /// </summary>
        public VisibilityBackup Vizibility { get; private set; }

        /// <summary>
        /// The original file name.
        /// </summary>
        public string OriginalFileName { get { return File; } }

        /// <summary>
        /// The user that uploaded a file.
        /// </summary>
        public string UploadedBy { get; private set; }

        public static CommandLineArguments Instance { get; private set; }

        /// <summary>
        /// Init based on the command line.
        /// </summary>
        static CommandLineArguments()
        {
            Instance = new CommandLineArguments();

            ConsoleCmdLine consoleCmdLine = new ConsoleCmdLine();

            CmdLineString commandLineString = new CmdLineString("command", true, "The command name");
            consoleCmdLine.RegisterParameter(commandLineString);

            CmdLineString userCmdLineString = new CmdLineString("user", true, "The user name");
            consoleCmdLine.RegisterParameter(userCmdLineString);

            CmdLineString passwordCmdLineString = new CmdLineString("passw", true, "The password for the given user");
            consoleCmdLine.RegisterParameter(passwordCmdLineString);

            CmdLineString descriptionCmdLineString = new CmdLineString("description", false, "The name of the file");
            consoleCmdLine.RegisterParameter(descriptionCmdLineString);

            CmdLineString nameCmdLineString = new CmdLineString("name", false, "The name of the file");
            consoleCmdLine.RegisterParameter(nameCmdLineString);

            CmdLineString fileCmdLineString = new CmdLineString("file", false, "The name of the file");
            consoleCmdLine.RegisterParameter(fileCmdLineString);

            CmdLineString vizibilityCmdLineString = new CmdLineString("vizibility", false, "The name of the file");
            consoleCmdLine.RegisterParameter(vizibilityCmdLineString);

            CmdLineString uploadedByCmdLineString = new CmdLineString("uploadedBy", false, "The name of the file");
            consoleCmdLine.RegisterParameter(uploadedByCmdLineString);

            CmdLineString parentId =new CmdLineString("parentTagId",false, "The id of the parent tag");
            consoleCmdLine.RegisterParameter(parentId);

            CmdLineString idLineString = new CmdLineString("Id", false, "The id of the entity.");
            consoleCmdLine.RegisterParameter(idLineString);

            consoleCmdLine.Parse(System.Environment.GetCommandLineArgs());

            Instance.Command = (CommandType)Enum.Parse(typeof(CommandType), commandLineString.Value);
            Instance.User = userCmdLineString.Value;
            Instance.Password = passwordCmdLineString.Value;
            Instance.File = fileCmdLineString.Value;
            Instance.Name = nameCmdLineString;
            Instance.Description = descriptionCmdLineString.Value;
            Instance.Vizibility = string.IsNullOrWhiteSpace(vizibilityCmdLineString.Value)
                ? VisibilityBackup.Private
                : (VisibilityBackup)Enum.Parse(typeof(VisibilityBackup), vizibilityCmdLineString);
            Instance.UploadedBy = uploadedByCmdLineString.Value;
            Instance.ParentTagId = parentId.Value;
            Instance.Id = idLineString.Value;
        }

    }
}
